import Router from "@/router";
import Vue from "vue";

let state = {
    /**
     * 添加路由，在仓库进行记录
     * @type { Map<string,{name:string,path:string,component:Vue,parent:string}> }
     */
    routerManage: new Map(),
}

let mutations = {
    /**
     * 添加唯一路由，不进行路由覆盖
     * @param {state} state
     * @param {{path:string,components:{name:Vue},parent:string}} route_huily 
     * @returns {Boolean}
     */
    AddRoute_HuiLY(state, route_huily) {
        // routeConfig
        let route = {
            name: route_huily.path,
            path: route_huily.path,
            components: route_huily.components,
        }
        if (route_huily.parent) {
            if (!state.routerManage.has(route_huily.path)) {
                state.routerManage.set(route_huily.path, Router.addRoute(route_huily.parent, route))
            }
        } else {
            if (!state.routerManage.has(route_huily.path)) {
                Router.addRoute(route)
                state.routerManage.set(route_huily.path, route)
            }
        }
        Router.push({
            path: route_huily.path
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations
}